//
//  NetWorkingGeneral.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/26.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "NetWorkingGeneral.h"

#import <AFHTTPSessionManager.h>

@implementation NetWorkingGeneral

//上传图片
+(void)uploadImageWithImage:(UIImage *)image withSuceessBlock:(void(^)(id responseObj))successBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
    [[XNProgressHUD shared] showWithTitle:@"正在上传图片..."];
    [manager POST:UploadImage parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //上传的参数(上传图片，以文件流的格式)
        NSData *data = UIImagePNGRepresentation(image);
        long nowDateStamp = [NSDate date].timeIntervalSince1970;
        [formData appendPartWithFileData:data name:@"imgFile" fileName:[NSString stringWithFormat:@"%ld.png",(long)nowDateStamp] mimeType:@"image/png"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [[XNProgressHUD shared] dismiss];
        NSString *imgUrl = responseObject[@"url"];
        successBlock(imgUrl);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
        }else{
            [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
        }
        successBlock(@"");
    }];
}




@end




